{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "c660687c-c2c7-473a-b4a0-444297870969",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "55597807-c9f7-4daa-9c3f-7c9f4ce358ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.random.uniform(0,4,(7,5)) # 生成0，4之间均匀分布的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "51107e2a-0d59-4aa5-84df-5caa602f1ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def min_max_normalization(data):\n",
    "    min_val = np.min(data)  # 获取当前数据集所有数据的最小值\n",
    "    max_val = np.max(data)  # 获取当前数据集所有数据的最大值\n",
    "    normalized_data = (data - min_val) / (max_val - min_val)  # 归一化公式\n",
    "    return normalized_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "cd42238c-8717-4826-95b5-edfea1667ff3",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_x=min_max_normalization(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "f1e83d4f-e2dc-48e7-8374-ea77d18f15bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.62485153 1.7233068  3.07389291 1.38671011 3.36189271]\n",
      " [1.23455158 3.69312144 2.4355488  3.65651707 0.53064381]\n",
      " [3.62405881 2.70537363 0.60014542 0.97003749 0.44452688]\n",
      " [3.64147918 1.98770391 1.35863835 3.59267809 3.62713761]\n",
      " [1.42130722 1.53907797 0.07521205 3.11362061 0.96457752]\n",
      " [3.39509371 1.38917142 3.85249787 0.26636239 1.48966391]\n",
      " [3.70299662 0.62200969 0.4175691  3.25277893 2.70704255]]\n",
      "\n",
      "\n",
      "[[0.41025211 0.43631719 0.79387184 0.34720647 0.87011701]\n",
      " [0.30692396 0.95780663 0.6248764  0.94811597 0.12057117]\n",
      " [0.93952296 0.69630992 0.13897105 0.23689641 0.09777254]\n",
      " [0.94413484 0.50631378 0.33977474 0.93121522 0.94033804]\n",
      " [0.35636572 0.38754439 0.         0.80438937 0.23545093]\n",
      " [0.87890666 0.34785807 1.         0.05060521 0.37446249]\n",
      " [0.96042099 0.1447594  0.09063573 0.84123019 0.69675175]]\n"
     ]
    }
   ],
   "source": [
    "print(x)\n",
    "print('\\n')\n",
    "print(n_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "6058c06c-063b-43a0-972b-c46bfbd8d601",
   "metadata": {},
   "outputs": [],
   "source": [
    "scaler=MinMaxScaler() #sklearn工具实现\n",
    "ns_x=scaler.fit_transform(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "cfdb2ff3-643c-41b5-860c-cb242b938c83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.62485153 1.7233068  3.07389291 1.38671011 3.36189271]\n",
      " [1.23455158 3.69312144 2.4355488  3.65651707 0.53064381]\n",
      " [3.62405881 2.70537363 0.60014542 0.97003749 0.44452688]\n",
      " [3.64147918 1.98770391 1.35863835 3.59267809 3.62713761]\n",
      " [1.42130722 1.53907797 0.07521205 3.11362061 0.96457752]\n",
      " [3.39509371 1.38917142 3.85249787 0.26636239 1.48966391]\n",
      " [3.70299662 0.62200969 0.4175691  3.25277893 2.70704255]]\n",
      "\n",
      "\n",
      "[[0.41025211 0.43631719 0.79387184 0.34720647 0.87011701]\n",
      " [0.30692396 0.95780663 0.6248764  0.94811597 0.12057117]\n",
      " [0.93952296 0.69630992 0.13897105 0.23689641 0.09777254]\n",
      " [0.94413484 0.50631378 0.33977474 0.93121522 0.94033804]\n",
      " [0.35636572 0.38754439 0.         0.80438937 0.23545093]\n",
      " [0.87890666 0.34785807 1.         0.05060521 0.37446249]\n",
      " [0.96042099 0.1447594  0.09063573 0.84123019 0.69675175]]\n"
     ]
    }
   ],
   "source": [
    "print(x)\n",
    "print('\\n')\n",
    "print(n_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "fe8d22a9-dde5-42f8-9fee-028d55d835fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1=np.array([1,-12,10086,7,5]) #10086为异常离群数据，使得结果大部份接近0，离群数据接近1\n",
    "x1=x1.reshape(-1,1) # 整形成列向量\n",
    "n_x1=scaler.fit_transform(x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "bc5c7056-ecee-4de3-8800-0b8831946e33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[    1]\n",
      " [  -12]\n",
      " [10086]\n",
      " [    7]\n",
      " [    5]]\n",
      "\n",
      "\n",
      "[[0.00128738]\n",
      " [0.        ]\n",
      " [1.        ]\n",
      " [0.00188156]\n",
      " [0.0016835 ]]\n"
     ]
    }
   ],
   "source": [
    "print(x1)\n",
    "print('\\n')\n",
    "print(n_x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "8b8ead92-468a-4307-85b4-c5742d2298b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 最大最小值归一化可使得数据在0，1之间，但是会受离群点影响\n",
    "# 归一化目的  让各个xi的量纲相差不大，让各个参数迭代幅度类似，避免离最优点近的参数快速迭代，离最优点远的慢速迭代\n",
    "# 有可能提升训练精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd6feca4-edc3-40bc-91eb-1a99fd618f34",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
